
import 'dart:core';
import 'package:flutter/foundation.dart';

enum AppErrorType {
  unknown,
  database,
  dataCorruption,
  parse,
  validation,
  permission,
  timeout,
  conflict,
  migration,
  storage,
  business,
  network,
}


class AppError extends Error {
  final String message;
  final AppErrorType type;
  final dynamic cause;
  final bool isRecoverable;
  @override
  final StackTrace? stackTrace;
  VoidCallback? retryCallback;

  AppError({
    required this.message,
    required this.type,
    this.cause,
    this.isRecoverable = false,
    this.stackTrace,
  });
  
  // 添加重试回调
  AppError withRetry(VoidCallback callback) {
    retryCallback = callback;
    return this;
  }

  
  factory AppError.unknownError({dynamic cause}) {
    return AppError(
      message: '发生未知错误',
      type: AppErrorType.unknown,
      cause: cause,
      isRecoverable: false,
      stackTrace: StackTrace.current,
    );
  }



  
  factory AppError.databaseError(String message, {dynamic cause}) {
    return AppError(
      message: message,
      type: AppErrorType.database,
      cause: cause,
      isRecoverable: false,
      stackTrace: StackTrace.current,
    );
  }

  
  factory AppError.fileError(String message, {dynamic cause}) {
    return AppError(
      message: message,
      type: AppErrorType.dataCorruption,
      cause: cause,
      isRecoverable: false,
      stackTrace: StackTrace.current,
    );
  }

  
  factory AppError.parseError(String message, {dynamic cause}) {
    return AppError(
      message: message,
      type: AppErrorType.parse,
      cause: cause,
      isRecoverable: false,
      stackTrace: StackTrace.current,
    );
  }

  
  factory AppError.validationError(String message) {
    return AppError(
      message: message,
      type: AppErrorType.validation,
      isRecoverable: true,
      stackTrace: StackTrace.current,
    );
  }

  
  factory AppError.migrationError(String message, {dynamic cause}) {
    return AppError(
      message: message,
      type: AppErrorType.migration,
      cause: cause,
      isRecoverable: true,
      stackTrace: StackTrace.current,
    );
  }

  
  factory AppError.permissionError(String message, {dynamic cause}) {
    return AppError(
      message: message,
      type: AppErrorType.permission,
      cause: cause,
      isRecoverable: true,
      stackTrace: StackTrace.current,
    );
  }

  
  factory AppError.timeoutError(String message, {dynamic cause}) {
    return AppError(
      message: message,
      type: AppErrorType.timeout,
      cause: cause,
      isRecoverable: true,
      stackTrace: StackTrace.current,
    );
  }

  
  factory AppError.conflictError(String message, {dynamic cause}) {
    return AppError(
      message: message,
      type: AppErrorType.conflict,
      cause: cause,
      isRecoverable: false,
      stackTrace: StackTrace.current,
    );
  }

  @override
  String toString() {
    return 'AppError($type): $message${cause != null ? ' (原因: $cause)' : ''}';
  }
}




extension AppErrorCopyWith on AppError {
  AppError copyWith({StackTrace? stackTrace}) {
    return AppError(
      message: message,
      type: type,
      cause: cause,
      isRecoverable: isRecoverable,
      stackTrace: stackTrace ?? this.stackTrace,
    );
  }
}